package sddz.foodmanage.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.zhy.autolayout.AutoLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import sddz.foodmanage.R;
import sddz.foodmanage.base.BaseActivity;
import sddz.foodmanage.fragment.HomeFragment;
import sddz.foodmanage.fragment.InpurchasingFragment;
import sddz.foodmanage.fragment.MyFragment;
import sddz.foodmanage.fragment.OrderformFragment;
import sddz.foodmanage.utils.ChangeFragment;
import sddz.foodmanage.utils.GlobalParms;
import sddz.foodmanage.utils.SpfUtils;
import sddz.foodmanage.utils.UpdateManager;
import sddz.foodmanage.view.BottomBar;

public class MainActivity extends BaseActivity {

    @BindView(R.id.fl_container)
    FrameLayout flContainer;
    @BindView(R.id.bottom_bar)
    BottomBar bottomBar;
    @BindView(R.id.activity_main)
    AutoLinearLayout activityMain;
    //声明一个long类型变量：用于存放上一点击“返回键”的时刻
    private long mExitTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initData() {
        GlobalParms.setFragmentSelected(new ChangeFragment() {
            @Override
            public void changge(int postion) {
                setSwPage(postion);
            }
        });


    }
    public void setSwPage(int i) {
        bottomBar.setFirstChecked(i);
//        bottomBar.setContainer(i);
//        bottomBar.setSelected(true);
    }

    private void initView() {
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }
        //管理员
        if(SpfUtils.getUserData(mActivity).getType()==1){//我的，订单
            bottomBar.setContainer(R.id.fl_container)
                    .setTitleBeforeAndAfterColor("#8a8a8a", "#CC0628")
                    .addItem(OrderformFragment.class,
                            "订单",  R.mipmap.order_before,
                            R.mipmap.order_select)
                    .addItem(MyFragment.class,
                            "我的",
                            R.mipmap.my_before,
                            R.mipmap.my_select)
                    .build();

        }else if(SpfUtils.getUserData(mActivity).getType()==2){//中队 商品浏览 采购中 订单 我的
            bottomBar.setContainer(R.id.fl_container)
                    .setTitleBeforeAndAfterColor("#8a8a8a", "#CC0628")
                    .addItem(HomeFragment.class, "商品浏览",
                            R.mipmap.spll_before,
                            R.mipmap.spll_select)
                    .addItem(InpurchasingFragment.class,
                            "采购中",
                            R.mipmap.cg_before,
                            R.mipmap.cg_select)
                    .addItem(OrderformFragment.class,
                            "订单",  R.mipmap.order_before,
                            R.mipmap.order_select)
                    .addItem(MyFragment.class,
                            "我的",
                            R.mipmap.my_before,
                            R.mipmap.my_select)
                    .build();
            bottomBar.setFirstChecked(3);
            bottomBar.setBottom(3);

        }else if(SpfUtils.getUserData(mActivity).getType()==3){//供应商 商品浏览 订单 我的
            bottomBar.setContainer(R.id.fl_container)
                    .setTitleBeforeAndAfterColor("#8a8a8a", "#CC0628")
                    .addItem(HomeFragment.class, "商品浏览",   R.mipmap.spll_before,
                            R.mipmap.spll_select)
                    .addItem(OrderformFragment.class,
                            "订单",  R.mipmap.order_before,
                            R.mipmap.order_select)
                    .addItem(MyFragment.class,
                            "我的",
                            R.mipmap.my_before,
                            R.mipmap.my_select)
                    .build();

        }

        UpdateManager updateManager = new UpdateManager(mActivity);
        updateManager.isUpdate();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断用户是否点击了“返回键”
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //与上次点击返回键时刻作差
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                //大于2000ms则认为是误操作，使用Toast进行提示
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                //并记录下本次点击“返回键”的时刻，以便下次进行判断
                mExitTime = System.currentTimeMillis();
            } else {
                //小于2000ms则认为是用户确实希望退出程序-调用System.exit()方法进行退出
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
